﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.CommandIds;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.Properties;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Represents an extension of SharePoint site nodes in Server Explorer for custom action groups. 
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeExtension))]
    // Indicates that this class extends SharePoint site nodes in Server Explorer.
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    class CustomActionGroupNodeSiteExtension : IExplorerNodeTypeExtension
    {
        #region Methods

        /// <summary>
        /// The NodeChildrenRequested event is raised when the user expands the SharePoint site node in Server Explorer.
        /// </summary>
        /// <param name="nodeType">The node type</param>
        public void Initialize(IExplorerNodeType nodeType)
        {
            //Bind the events
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }

        /// <summary>
        /// Creates the new Custom Action Groups node with the specified icon.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeEventArgs object</param>
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            // The CreateCustomActionNodes argument is a delegate that Visual Studio calls 
            // to create the child nodes under the Custom Action Groups node.
            GenericFolderNode.Create(e.Node, "Custom Action Groups",
                Resources.CustomActionGroupsNode.ToBitmap(), 
                CreateCustomActionGroupNodes);
        }

        /// <summary>
        /// Creates all of the individual custom action group nodes under the new custom action groups node.
        /// </summary>
        /// <param name="parentNode">The parent node</param>
        private void CreateCustomActionGroupNodes(IExplorerNode parentNode)
        {
            // Call the custom SharePoint command to get items from the custom action groups.
            var customActionGroups = 
                parentNode.Context.SharePointConnection.ExecuteCommand<CustomActionGroupNodeInfo[]>(CustomActionGroupCommandIds.GetCustomActionGroups);

            if (customActionGroups != null)
            {
                foreach (CustomActionGroupNodeInfo customActionGroup in customActionGroups)
                {
                    // Create a new annotation object to store the current custom action group item with the new node.
                    var annotations = new Dictionary<object, object>() 
                    { 
                        { typeof(CustomActionGroupNodeInfo), customActionGroup } 
                    };

                    // Create the new node for the current CustomActionGroup item.
                    parentNode.ChildNodes.Add(CustomActionGroupNodeTypeProvider.CustomActionGroupNodeTypeId,
                        customActionGroup.Name, 
                        annotations);
                }
            }
        }

        #endregion

    }
}
